package com.google.code.retrobox.editor.commons.dialog;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.*;

import com.google.code.retrobox.editor.commons.Tools;


public class ColorReplaceDialog extends JDialog implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<ActionListener> listeners = new LinkedList<ActionListener>();
	
	// Components
	private JPanel	oldColorPanel = new JPanel(new GridLayout(17, 1)),
					newColorPanel = new JPanel(new GridLayout(17, 1));
	
	private JButton		okButton = new JButton("OK!"),
						cancelButton = new JButton("Cancel");
	
	// Data
	private Color oldColor = Color.black;
	private Color newColor = Color.black;
	
	
	public ColorReplaceDialog(JFrame parent) {
		super(parent, false);
		init();
	}
	
	public void init() {
		setLayout(new BorderLayout());
		
		ButtonGroup oldGroup = new ButtonGroup();
		ButtonGroup newGroup = new ButtonGroup();
		
		oldColorPanel.add(new JLabel("Old color"));
		newColorPanel.add(new JLabel("New color"));
		
		// Add old colors
		for (int i = 0;i < 16;i++) {
			JPanel tmpPanel = new JPanel(new BorderLayout());
			JRadioButton bt = new JRadioButton();
			final int a = i;
			bt.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					oldColor = Tools.getColor(a);
				}
			});
			oldGroup.add(bt);
			if (i == 0) {
				bt.setSelected(true);
			}
			
			JPanel p = new JPanel();
			p.setOpaque(true);
			p.setBackground(Tools.getColor(i));
			p.setPreferredSize(new Dimension(70, 24));
			
			tmpPanel.add(bt, BorderLayout.WEST);
			tmpPanel.add(p, BorderLayout.CENTER);
			
			oldColorPanel.add(tmpPanel);
		}
		
		// Add new colors
		for (int i = 0;i < 16;i++) {
			JPanel tmpPanel = new JPanel(new BorderLayout());
			JRadioButton bt = new JRadioButton();
			final int a = i;
			bt.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					newColor = Tools.getColor(a);
				}
			});
			newGroup.add(bt);
			if (i == 0) {
				bt.setSelected(true);
			}
			
			JPanel p = new JPanel();
			p.setOpaque(true);
			p.setBackground(Tools.getColor(i));
			p.setPreferredSize(new Dimension(70, 25));
			
			tmpPanel.add(bt, BorderLayout.WEST);
			tmpPanel.add(p, BorderLayout.CENTER);
			
			newColorPanel.add(tmpPanel);
		}
		
		okButton.addActionListener(this);
		cancelButton.addActionListener(this);
		
		JPanel tmp = new JPanel(new BorderLayout());
		tmp.add(okButton, BorderLayout.WEST);
		tmp.add(cancelButton, BorderLayout.EAST);
		
		add(oldColorPanel, BorderLayout.WEST);
		add(newColorPanel, BorderLayout.EAST);
		add(tmp, BorderLayout.SOUTH);
		
		setTitle("Replace colors");
		pack();
		setLocationRelativeTo(null);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
	}
	
	public Color getOldColor() {
		return oldColor;
	}
	
	public Color getNewColor() {
		return newColor;
	}
	
	public void addActionListener(ActionListener listener) {
		listeners.add(listener);
	}

	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == okButton) {
			for (ActionListener element: listeners) {
				element.actionPerformed(new ActionEvent(this, 1, "Colors selected"));
			}
			this.dispose();
			
		} else if (arg0.getSource() == cancelButton) {
			this.dispose();
		}
	}
}












